var express = require('express');
var router = express.Router();

const Point = require("../models/point");
router.get('/', async function(req, res, next) {
    const points = await Point.find()
    res.status(200).json(points);
});
router.get('/home', async function(req, res, next) {
    const points = await Point.find().limit(3).lean();
    res.status(200).json(points);
});


router.post('/create', async function(req, res, next) {
    const point = await Point.create(req.body.point);
    res.status(200).json(
        {
            result:"创建成功",
            point:point
        }
        
    )
});
    
    router.post('/delete', async function(req, res, next) {
        const point = await Point.findOneAndDelete({fid:req.body.point.fid});
        res.status(200).json(
            {
                result:"删除成功",
    
            }
            
        )
    });
    
    router.post('/update', async function(req, res, next) {
        const point = await Point.findOneAndUpdate({fid:req.body.point.fid},req.body.point);
        res.status(200).json(
            {
                result:"修改成功",
    
            }
            
        )
    });

    module.exports = router;


    // router.get('/:fid', function(req, res, next) {
//     const point = points.find(item => item.fid == req.params.fid)
//     res.status(200).json(point);
// });


// /* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });

// const points =[
//     {"id":"1",
//     "name": "我",
//     "desc": "bl聪明",
//     "area":"我",
//     "tag1": "美",
//     "tag2": "啦",
//     "tags": ["a","b","c"],
//     "image":"../../assets/images/风景1.jpg",
//     "geometry":{
//       "type":"Point",
//       "coordinates":[ 120.14354, 30.371176 ] 
//     }
//   },
//     {
//       "id":"2",
//     "name":"真",
//     "desc": "cjy笨",
//     "area":"你",
//     "tag1": "好",
//     "tag2": "啊",
//     "tags":["a","d","e"],
//     "image":"../../assets/images/风景2.jpg",
//     "geometry":{
//       "type":"Point",
//       "coordinates":[ 120.143551, 30.371456 ] 
//     }
//   },
//     {
//       "id":"3",
//     "name": "美",
//     "desc": "lzy和ccx蠢",
//     "area":"我",
//     "tag1": "真",
//     "tag2": "牛",
//     "tags": ["a","f","g"],
//     "image":"../../assets/images/风景3.jpg",
//     "geometry":{
//       "type":"Point",
//       "coordinates":[ 120.144176, 30.371491 ] 
//     }
//     }]

    // router.get('/', function(req, res, next) {
    //     res.status(200).json(points);
    // });
    // router.get('/fid/:fid', function(req, res, next) {
    //     const point = points.find(item => item.id == req.params.fid)
    //     res.status(200).json(point);
    // });

    // router.get('/name/:name', function(req, res, next) {
    //     const point = points.find(item => item.name == req.params.name)
    //     res.status(200).json(point);
    // });

    // router.get('/area/:area', function(req, res, next) {
    //     const array = points.filter(item => item.area == req.params.area)
    //     res.status(200).json(array);
    // });